#include "movejoint.h"

MoveJoint::MoveJoint(QObject *parent)
    : Operation{parent}
{
    m_dblJointAngles.append(0.0);
    m_dblJointAngles.append(0.0);
    m_dblJointAngles.append(0.0);
    m_dblJointAngles.append(0.0);
    m_dblJointAngles.append(0.0);
    m_dblJointAngles.append(0.0);
}

void MoveJoint::run(){
    qDebug() << "MoveJoint is Running!";
    qDebug() << m_dblJointAngles;
}

void MoveJoint::writeXML(QXmlStreamWriter& writer){
    // 写入当前操作的信息
    writer.writeStartElement("MoveJoint");
    writer.writeAttribute("device", m_strDeviceID);
    int index = 1;
    foreach(double value, m_dblJointAngles){
        writer.writeTextElement(QString("joint%1").arg(index++), QString::number(value));
    }

    writer.writeEndElement();  // 结束当前操作的元素
}

void MoveJoint::readFromXML(QXmlStreamReader& reader){
    QXmlStreamAttributes attributes = reader.attributes();
    if(attributes.hasAttribute("device")) {
        m_strDeviceID = attributes.value("device").toString();
    }

    m_dblJointAngles.clear();
    QXmlStreamReader::TokenType token = reader.readNext();
    while (!(reader.tokenType() == QXmlStreamReader::EndElement &&
             reader.name() == QStringLiteral("MoveJoint"))) {

        if (reader.name() == QStringLiteral("joint1") && reader.isStartElement()) {
            m_dblJointAngles.append(reader.readElementText().toDouble());
        }else if (reader.name() == QStringLiteral("joint2") && reader.isStartElement()) {
            m_dblJointAngles.append(reader.readElementText().toDouble());
        }else if (reader.name() == QStringLiteral("joint3") && reader.isStartElement()) {
            m_dblJointAngles.append(reader.readElementText().toDouble());
        }else if (reader.name() == QStringLiteral("joint4") && reader.isStartElement()) {
            m_dblJointAngles.append(reader.readElementText().toDouble());
        }else if (reader.name() == QStringLiteral("joint5") && reader.isStartElement()) {
            m_dblJointAngles.append(reader.readElementText().toDouble());
        }else if (reader.name() == QStringLiteral("joint6") && reader.isStartElement()) {
            m_dblJointAngles.append(reader.readElementText().toDouble());
        }

        reader.readNext();
    }
}
